import 'dart:io';
import 'package:xsdk_flutter/xsdk/framework/bean/x_bean_device.dart';
import 'package:xsdk_flutter/xsdk/operation/x_constant_temp.dart';
import 'package:xsdk_flutter/xsdk/utils/queue/x_bean_queue_message.dart';
import 'package:xsdk_flutter/xsdk/utils/json/x_utils_json.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_share_preferences.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';

/*
远程设备(api方式通讯),如: 快麦云打印
注意:这里不做队列,直接在外层遍历设备处理(参照蓝牙,socket)
 */

abstract class XRemoteDeviceUtils{

  //连接设备
  static final Set<XBaseDevice> remoteDeviceList={};

  /*
  1.初始化 ok(注意:不能在app中初始化,因为sp初始化没完成,导致获取设备记录为空)
  注意:
  1.1.如果使用多用户保存:
  则需要登录后更新用户组:XSharePreferencesUtils.saveString(XConstants.SP_UserGroup,XXX);
  然后再初始化;切换用户或company时,则需要重新初始化
  1.2.如果不启用多用户,则可在欢迎界面初始化;
   */
  static void init(){
    //
    remoteDeviceList.clear();

    //1.连接设备
    String userGroup=XSharePreferencesUtils.getString(XConstants.SP_UserGroup);//多用户保存
    String connectDeviceJson=XSharePreferencesUtils.getString('$userGroup${XConstants.SP_RemoteDevicesList}');

    if(!XStringUtils.isEmpty(connectDeviceJson)){
      List<dynamic> list=XJsonUtils.json2List(connectDeviceJson);
      for(dynamic item in list){
        XBaseDevice baseDevice=XBaseDevice.fromJson(item);
        remoteDeviceList.add(baseDevice);
      }
    }

    XLogUtils.printLog('---远程设备记录数=${remoteDeviceList.length}');

  }


  //2.添加设备记录和连接ok
  static void addDevice(XBaseDevice baseDevice){

    try{
      //
      if(XStringUtils.isEmpty(baseDevice.deviceUUID)){
        XLogUtils.printLog('---远程设备Id为空');
        return;
      }

      //1.保存连接设备记录
      if(!remoteDeviceList.contains(baseDevice)){
        XLogUtils.printLog('---添加设备记录');
        remoteDeviceList.add(baseDevice);
        //XLogUtils.printLog("---当前设备数:${connectedDeviceList.length}");
        String userGroup=XSharePreferencesUtils.getString(XConstants.SP_UserGroup);//多用户保存
        XSharePreferencesUtils.saveString('$userGroup${XConstants.SP_RemoteDevicesList}', XJsonUtils.obj2Json(remoteDeviceList));

      }else{
        XLogUtils.printLog('---添加远程设备已存在');
      }

    }catch(e){
      XLogUtils.printLog('connectDevice报错: $e');
    }

  }

  //3.更新设备设置 ok
  static void updateDevice(XBaseDevice device){

    if(XStringUtils.isEmpty(device.deviceUUID)){
      XLogUtils.printLog('---远程设备Id为空');
      return;
    }

    for(XBaseDevice deviceItem in remoteDeviceList){
      if(device==deviceItem){
        deviceItem.deviceName=device.deviceName;
        deviceItem.nickName=device.nickName;
        deviceItem.deviceKey=device.deviceKey;
        deviceItem.brand=device.brand;
        deviceItem.isConnected=device.isConnected;
        deviceItem.isUserSelect=device.isUserSelect;
        deviceItem.isEnable=device.isEnable;
        deviceItem.customType=device.customType;
        deviceItem.isServerSocket=device.isServerSocket;
        deviceItem.port=device.port;
        deviceItem.ip=device.ip;
        deviceItem.isGBK=device.isGBK;
        deviceItem.isSocketShortConnect=device.isSocketShortConnect;
        deviceItem.isMTULongWrite=device.isMTULongWrite;

        deviceItem.scenes =device.scenes;
        deviceItem.printerPagerSize=device.printerPagerSize;
        deviceItem.serialNumber=device.serialNumber;

        deviceItem.printCommandType=device.printCommandType;
        deviceItem.printTypeName=device.printTypeName;
        deviceItem.printerDPI=device.printerDPI;
        deviceItem.printerLabelSizeWidth=device.printerLabelSizeWidth;
        deviceItem.printerLabelSizeHeight=device.printerLabelSizeHeight;

        break;
      }
    }

    //更新保存
    String userGroup=XSharePreferencesUtils.getString(XConstants.SP_UserGroup);//多用户保存
    XSharePreferencesUtils.saveString('$userGroup${XConstants.SP_RemoteDevicesList}', XJsonUtils.obj2Json(remoteDeviceList));

  }


  //4.移除设备并删除记录 ok
  static void removeDevice(XBaseDevice device){
    //移除设备记录
    remoteDeviceList.remove(device);

    //更新保存
    String userGroup=XSharePreferencesUtils.getString(XConstants.SP_UserGroup);//多用户保存
    XSharePreferencesUtils.saveString('$userGroup${XConstants.SP_RemoteDevicesList}', XJsonUtils.obj2Json(remoteDeviceList));

  }

  //5.删除设备by customType
  static void removeDeviceByType(String deviceCustomType){
    //断开和删除连接, 客户端(适用多个)
    remoteDeviceList.removeWhere((XBaseDevice device){
      if(deviceCustomType==device.customType){
        return true;
      }else{
        return false;
      }
    });

    //更新保存
    String userGroup=XSharePreferencesUtils.getString(XConstants.SP_UserGroup);//多用户保存
    XSharePreferencesUtils.saveString('$userGroup${XConstants.SP_RemoteDevicesList}', XJsonUtils.obj2Json(remoteDeviceList));

  }


}